990. 等式方程的可满足性
为保证权益,题目请参考 990. 等式方程的可满足性(From LeetCode).
解决方案1
CPP
C++
//
// Created by lenovo on 2020/6/8.
//
/**
* 990. 等式方程的可满足性
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
int par[26];
int rank[26];
int find(int i) {
if (this->par[i] == i) {
return i;
} else {
return this->par[i] = find(this->par[i]);
}
}
void unionit(int a, int b) {
a = this->find(a);
b = this->find(b);
if (this->rank[a] < this->rank[b]) {
this->par[a] = b;
} else {
this->par[b] = a;
if (this->rank[a] == this->rank[b]) {
this->rank[a] += 1;
}
}
}
bool same(int a, int b) {
return this->find(a) == this->find(b);
}
bool equationsPossible(vector <string> &equations) {
for (int i = 0; i < 26; ++i) {
this->par[i] = i;
this->rank[i] = 0;
}
for (string equation: equations) {
if (equation[1] == '=') {
int a = int(equation[0] - 'a');
int b = int(equation[3] - 'b');
this->unionit(a, b);
}
}
for (string equation: equations) {
if (equation[1] == '!') {
int a = int(equation[0] - 'a');
int b = int(equation[3] - 'b');
if (this->same(a, b)) {
return false;
}
}
}
return true;
}
};
int main() {
vector<string> vec;
vec.push_back("a==b");
vec.push_back("a!=b");
Solution so;
cout << so.equationsPossible(vec) << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82